package com.atguigu.yygh.order.service.impl;

import com.atguigu.yygh.enums.RefundStatusEnum;
import com.atguigu.yygh.model.order.PaymentInfo;
import com.atguigu.yygh.model.order.RefundInfo;
import com.atguigu.yygh.order.mapper.RefundInfoMapper;
import com.atguigu.yygh.order.service.RefundInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @Author:hx
 * @DateTime:2022/11/20 14:44
 * @Description:
 */
@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {
    @Override
    public RefundInfo saveRefundInfo(Map<String, String> xmlToMap, PaymentInfo paymentInfo) {
        RefundInfo refundInfo;
        //修改退款记录
        if (xmlToMap == null){
            QueryWrapper<RefundInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("order_id",paymentInfo.getOrderId());
            refundInfo = baseMapper.selectOne(queryWrapper);
            if (refundInfo == null){
                refundInfo= new RefundInfo();
                refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
                refundInfo.setOrderId(paymentInfo.getOrderId());
                refundInfo.setPaymentType(paymentInfo.getPaymentType());
                refundInfo.setTotalAmount(paymentInfo.getTotalAmount());
                refundInfo.setSubject("尚医通预约费用退款");
                refundInfo.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());
                baseMapper.insert(refundInfo);
                return refundInfo;
            }
            return refundInfo;
        }else {
            UpdateWrapper<RefundInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("order_id",paymentInfo.getOrderId());
            refundInfo = new RefundInfo();
            refundInfo.setTradeNo(xmlToMap.get("transaction_id"));
            refundInfo.setCallbackContent(xmlToMap.toString());
            refundInfo.setRefundStatus(RefundStatusEnum.REFUND.getStatus());
            refundInfo.setCallbackTime(new Date());
            baseMapper.update(refundInfo,updateWrapper);
            return null;
        }
    }
}
